<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>广平网</title>
		<script src="js/mui.min.js"></script>
		<link href="css/mui.min.css" rel="stylesheet" />
		<style>
			body {
				min-height: 100%;
			}
		</style>
		<script type="text/javascript" charset="utf-8">
			mui.init();
			var wgtVer = null;
			mui.plusReady(function() {

				// 获取本地应用资源版本号
				plus.runtime.getProperty(plus.runtime.appid, function(inf) {
					wgtVer = inf.version;
					console.log("当前应用版本：" + wgtVer);

				});

				checkUpdate();

				//获取当前title和当前webview
				navTitle = document.querySelector('.mui-title');
				mainWebview = plus.webview.currentWebview();

				//设置状态栏颜色
				//plus.navigator.setStatusBarBackground("#fff");
				plus.navigator.setStatusBarStyle("dark");
				

				//设置只支持竖屏幕显示
				plus.screen.lockOrientation("portrait-primary");
				//初始化与index有关的页面
				initIndexView();

			});

			var navTitle;
			var mainWebview;
			var curBarItemWebview;

			var barItemWebviewArray = [];
			var barItemArray = ['pages/home.html', 'pages/my.html'];

			// 检测更新
			var checkUrl = "http://wfapi.ziyouyu.cn/app/v.php";

			function checkUpdate() {
				mui.toast('正在检测更新……');
				var xhr = new XMLHttpRequest();
				xhr.onreadystatechange = function() {
					switch(xhr.readyState) {
						case 4:
							plus.nativeUI.closeWaiting();
							if(xhr.status == 200) {
								console.log("检测更新成功：" + xhr.responseText);
								var newVer = xhr.responseText;
								if(wgtVer && newVer && (wgtVer != newVer)) {
									downWgt(); // 下载升级包
								} else {
									mui.toast('当前版本为最新版本！');
								}
							} else {
								console.log("检测更新失败！");
								mui.toast('检测更新失败！');
							}
							break;
						default:
							break;
					}
				}
				xhr.open('GET', checkUrl);
				xhr.send();
			}
			// 下载wgt文件
			var wgtUrl = "http://wfapi.ziyouyu.cn/app/H52D5955B.wgt";

			function downWgt() {
				plus.nativeUI.showWaiting("下载wgt文件...");
				plus.downloader.createDownload(wgtUrl, {
					filename: "_doc/update/"
				}, function(d, status) {
					if(status == 200) {
						console.log("下载wgt成功：" + d.filename);
						installWgt(d.filename); // 安装wgt包
					} else {
						console.log("下载wgt失败！");
						plus.nativeUI.alert("下载wgt失败！");
					}
					plus.nativeUI.closeWaiting();
				}).start();
			}
			// 更新应用资源
			function installWgt(path) {
				plus.nativeUI.showWaiting("安装wgt文件...");
				plus.runtime.install(path, {}, function() {
					plus.nativeUI.closeWaiting();
					console.log("安装wgt文件成功！");
					plus.nativeUI.alert("应用资源更新完成！", function() {
						plus.runtime.restart();
					});
				}, function(e) {
					plus.nativeUI.closeWaiting();
					console.log("安装文件失败[" + e.code + "]：" + e.message);
					plus.nativeUI.alert("安装文件失败[" + e.code + "]：" + e.message);
				});
			}
			//初始化
			function initIndexView() {
				//初始化所有bar页面
				inittabitemWebviews();
				//添加bar点击事件接受
				tapBaritem();
			}

			//初始化所有bar页面
			function inittabitemWebviews() {
				for(var i = 0; i < barItemArray.length; i++) {
					barItemWebviewArray[i] = mui.preload({
						id: barItemArray[i],
						url: barItemArray[i],
						styles: {
							top: '45px',
							bottom: '51px',
							left: '0px',
							bounce: 'vertical',
							bounceBackground: '#f8f8f8'
						},
						waiting: {
							autoShow: false
						}
					});
					barItemWebviewArray[i].hide();
					mainWebview.append(barItemWebviewArray[i]);
				}
				barItemWebviewArray[0].show();
				curBarItemWebview = barItemWebviewArray[0];
			}
			//添加点击事件
			function tapBaritem() {
				mui('.mui-bar-tab').on('tap', '.mui-tab-item', function() {
					var baritem = this;
					var baritemurl = baritem.getAttribute('href');
					//indexof()如果为false返回－1所以前面加上~
					if(!~curBarItemWebview.getURL().indexOf(baritemurl)) {
						for(var i = 0; i < barItemArray.length; i++) {
							if(barItemArray[i] == baritemurl) {
								//更改头部名字
								navTitle.innerText = baritem.children[baritem.children.length - 1].innerText;
								//判断是不是首页，如果是首页，名字改为广平网
								if(navTitle.innerText == '首页') {
									navTitle.innerText = '广平网';
								}

								//切换baritemwebview
								barItemWebviewArray[i].show();
								curBarItemWebview = barItemWebviewArray[i];
								break;
							}
						}
					}
				});
			}
		</script>
	</head>

	<body>
		<header class="mui-bar mui-bar-nav">
			<h1 class="mui-title">广平网</h1>
		</header>
		<div class="mui-content">

		</div>

		<nav class="mui-bar mui-bar-tab">
			<a class="mui-tab-item mui-active" href="pages/home.html">
				<span class="mui-icon mui-icon-home"></span>
				<span class="mui-tab-label">首页</span>
			</a>
			<a class="mui-tab-item" href="pages/my.html">
				<span class="mui-icon mui-icon-gear"></span>
				<span class="mui-tab-label">设置</span>
			</a>
		</nav>

	</body>

</html>